﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using devtm.Editor.Configuration.formWpf;

namespace devtm.Editor.Configuration.formWpf
{
    /// <summary>
    /// Interaction logic for InputBox.xaml
    /// </summary>
    public partial class InputBox : Window
    {
        private InputBox()
        {
            InitializeComponent();
        }


        private bool Retour = false;
        private List<InputBoxValidator> DelegateValidator = new List<InputBoxValidator>();


        # region Call


        /// <summary>
        /// Shows the dialog form for ask a string value
        /// </summary>
        /// <param name="messageBoxText">show a information message in the form</param>
        /// <param name="caption">The caption. is the title of the form</param>
        /// <param name="defaultValue">The default value.</param>
        /// <param name="validators">The validator.</param>
        /// <param name="errorMessage">The error message. is showed if the validator return <c>false</c></param>
        /// <returns><c>String</c> value</returns>
        /// <remarks>if youc click on cancel, the return is empty</remarks>
        public static String ShowDialog(String messageBoxText, String caption, String defaultValue, params InputBoxValidator[] validators)
        {

            InputBox frm = new InputBox() { Title = caption };

            foreach (InputBoxValidator item in validators)
                frm.DelegateValidator.Add(item);
                
            frm.label1.Content = messageBoxText;
            frm.textBox1.Text = defaultValue;

            frm.ShowDialog();

            if (frm.Retour)
                return frm.textBox1.Text;

            return String.Empty;

        }




        /// <summary>
        /// Shows the dialog form for ask a string value
        /// </summary>
        /// <param name="messageBoxText">show a information message in the form</param>
        /// <param name="caption">The caption. is the title of the form</param>
        /// <param name="defaultValue">The default value.</param>
        /// <param name="validator">The validator.</param>
        /// <param name="errorMessage">The error message. is showed if the validator return <c>false</c></param>
        /// <returns><c>String</c> value</returns>
        /// <remarks>if youc click on cancel, the return is empty</remarks>
        public static String ShowDialog(String messageBoxText, String caption, String defaultValue, Func<String, bool> validator, string errorMessage)
        {

            InputBox frm = new InputBox() { Title = caption };

            frm.DelegateValidator.Add(new InputBoxValidator(validator, errorMessage));
            frm.label1.Content = messageBoxText;
            frm.textBox1.Text = defaultValue;

            frm.ShowDialog();

            if (frm.Retour)
                return frm.textBox1.Text;

            return String.Empty;

        }


        /// <summary>
        /// Shows the dialog form for ask a string value
        /// </summary>
        /// <param name="messageBoxText">show a information message in the form</param>
        /// <param name="caption">The caption. is the title of the form</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns><c>String</c> value</returns>
        /// <remarks>if youc click on cancel, the return is empty</remarks>
        public static String ShowDialog(String messageBoxText, String caption, String defaultValue) 
        {
            return ShowDialog(messageBoxText, caption, defaultValue, String.Empty, String.Empty); 
        }


        /// <summary>
        /// Shows the dialog form for ask a string value
        /// </summary>
        /// <param name="messageBoxText">show a information message in the form</param>
        /// <param name="caption">The caption. is the title of the form</param>
        /// <param name="defaultValue">The default value.</param>
        /// <param name="RegExMatchValidator">The reg ex match validator.</param>
        /// <param name="errorMessage">The error message. is showed if the validator return <c>false</c></param>
        /// <returns><c>String</c> value</returns>
        /// <remarks>if youc click on cancel, the return is empty</remarks>
        public static String ShowDialog(String messageBoxText, String caption, String defaultValue, String RegExMatchValidator, string errorMessage) 
        { 
            return ShowDialog(messageBoxText, caption, defaultValue, (c) => Regex.IsMatch(c, RegExMatchValidator), errorMessage); 
        }


        #endregion

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {

            label2.Visibility = ImgError.Visibility = Visibility.Hidden;
            button1.IsEnabled = true;

            
            foreach (InputBoxValidator item in DelegateValidator)
            {
                bool b = (bool)item.DelegateValidator(textBox1.Text);
                if (!b)
                {
                    label2.Visibility = ImgError.Visibility = Visibility.Visible;
                    button1.IsEnabled = false;

                    label2.Content = item.MessageError;

                }
               
            }
 
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Retour = true;
            this.Close();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter || e.Key == Key.Return)
            {
                Retour = true;
                this.Close();
            }
        }

    }
}
